package unit

import (
	"fmt"
	"regexp"
)

// 手机正则验证容量
var phoneRegexp = []map[string]string{
	{"iso": "DZ", "code": "213", "pattern": `^(\+?213|0)(5|6|7)\d{8}$`},                                                                       // 213，阿尔及利亚
	{"iso": "SY", "code": "963", "pattern": `^(!?(\+?963)|0)?9\d{8}$`},                                                                        // 963，叙利亚
	{"iso": "SA", "code": "966", "pattern": `^(!?(\+?966)|0)?5\d{8}$`},                                                                        // 966，沙特阿拉伯
	{"iso": "US", "code": "1", "pattern": `^(\+?1)?[2-9]\d{2}[2-9](?!11)\d{6}$`},                                                              // 1，美国、加拿大
	{"iso": "CZ", "code": "420", "pattern": `^(\+?420)? ?[1-9][0-9]{2} ?[0-9]{3} ?[0-9]{3}$`},                                                 // 420，捷克共和国
	{"iso": "DE", "code": "49", "pattern": `^(\+?49[ \.\-])?([\(]{1}[0-9]{1,6}[\)])?([0-9 \.\-\/]{3,20})((x|ext|extension)[ ]?[0-9]{1,4})?$`}, // 49，德国
	{"iso": "DK", "code": "45", "pattern": `^(\+?45)?(\d{8})$`},                                                                               // 45，丹麦
	{"iso": "GR", "code": "30", "pattern": `^(\+?30)?(69\d{8})$`},                                                                             // 30，希腊
	{"iso": "AU", "code": "61", "pattern": `^(\+?61|0)4\d{8}$`},                                                                               // 61，澳大利亚
	{"iso": "GB", "code": "44", "pattern": `^(\+?44|0)7\d{9}$`},                                                                               // 44，英国
	{"iso": "HK", "code": "852", "pattern": `^(\+?852\-?)?[569]\d{3}\-?\d{4}$`},                                                               // 852，中国香港特别行政区
	{"iso": "IN", "code": "91", "pattern": `^(\+?91|0)?[789]\d{9}$`},                                                                          // 91，印度
	{"iso": "NZ", "code": "64", "pattern": `^(\+?64|0)2\d{7,9}$`},                                                                             // 64，新西兰
	{"iso": "ZA", "code": "27", "pattern": `^(\+?27|0)\d{9}$`},                                                                                // 27，南非
	{"iso": "ZM", "code": "260", "pattern": `^(\+?26)?09[567]\d{7}$`},                                                                         // 260，赞比亚
	{"iso": "ES", "code": "34", "pattern": `^(\+?34)?(6\d{1}|7[1234])\d{7}$`},                                                                 // 34，西班牙
	{"iso": "FI", "code": "358", "pattern": `^(\+?358|0)\s?(4(0|1|2|4|5)?|50)\s?(\d\s?){4,8}\d$`},                                             // 358，芬兰
	{"iso": "FR", "code": "33", "pattern": `^(\+?33|0)[67]\d{8}$`},                                                                            // 33，法国
	{"iso": "IL", "code": "972", "pattern": `^(\+972|0)([23489]|5[0248]|77)[1-9]\d{6}/`},                                                      // 972，以色列
	{"iso": "HU", "code": "36", "pattern": `^(\+?36)(20|30|70)\d{7}$`},                                                                        // 36，匈牙利
	{"iso": "IT", "code": "39", "pattern": `^(\+?39)?\s?3\d{2} ?\d{6,7}$`},                                                                    // 39，意大利
	{"iso": "JP", "code": "81", "pattern": `^(\+?81|0)\d{1,4}[ \-]?\d{1,4}[ \-]?\d{4}$`},                                                      // 81，日本
	{"iso": "MY", "code": "60", "pattern": `^(\+?6?01){1}(([145]{1}(\-|\s)?\d{7,8})|([236789]{1}(\s|\-)?\d{7}))$`},                            // 60，马来西亚
	{"iso": "BE", "code": "32", "pattern": `^(\+?32|0)4?\d{8}$`},                                                                              // 32，比利时
	{"iso": "NO", "code": "47", "pattern": `^(\+?47)?[49]\d{7}$`},                                                                             // 47，挪威
	{"iso": "PL", "code": "48", "pattern": `^(\+?48)? ?[5-8]\d ?\d{3} ?\d{2} ?\d{2}$`},                                                        // 48，波兰
	{"iso": "BR", "code": "55", "pattern": `^(\+?55|0)\-?[1-9]{2}\-?[2-9]{1}\d{3,4}\-?\d{4}$`},                                                // 55，巴西
	{"iso": "PT", "code": "351", "pattern": `^(\+?351)?9[1236]\d{7}$`},                                                                        // 351，葡萄牙
	{"iso": "RU", "code": "7", "pattern": `^(\+?7|8)?9\d{9}$`},                                                                                // 7，俄罗斯
	{"iso": "RS", "code": "381", "pattern": `^(\+3816|06)[- \d]{5,9}$`},                                                                       // 381，塞尔维亚
	{"iso": "TR", "code": "90", "pattern": `^(\+?90|0)?5\d{9}$`},                                                                              // 90，土耳其
	{"iso": "VN", "code": "84", "pattern": `^(\+?84|0)?((1(2([0-9])|6([2-9])|88|99))|(9((?!5)[0-9])))([0-9]{7})$`},                            // 84，越南
	{"iso": "CN", "code": "86", "pattern": `^(\+?0?86\-?)?1[3456789]\d{9}$`},                                                                  // 86，中国
	{"iso": "TW", "code": "886", "pattern": `^(\+?886\-?|0)?9\d{8}$`},                                                                         // 886，中国台湾
}
var phoneCodeRegexp = make(map[string]string)
var isoCodeRegexp = make(map[string]string)

func init() {
	for _, v := range phoneRegexp {
		phoneCodeRegexp[v["code"]] = v["pattern"]
		isoCodeRegexp[v["iso"]] = v["pattern"]
	}
}

// ValidateString 验证字符串是否有效
func ValidateString(str string, min int, max ...int) bool {
	pattern := fmt.Sprintf(`^([a-zA-Z0-9]{%d})$`, min)
	if len(max) != 0 {
		pattern = fmt.Sprintf(`^([a-zA-Z0-9]{%d,%d})$`, min, max[0])
	}
	reg := regexp.MustCompile(pattern)
	return reg.MatchString(str)
}

// ValidateMd5 验证 MD5 加密字符串是否有效
func ValidateMd5(md5 string) bool {
	pattern := `^([a-fA-F0-9]{32})$`
	reg := regexp.MustCompile(pattern)
	return reg.MatchString(md5)
}

// ValidateEmail 验证邮箱字符串是否有效
func ValidateEmail(email string) bool {
	//^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$
	pattern := `[A-Za-z0-9]+([_\.][A-Za-z0-9]+)*@([A-Za-z0-9\-]+\.)+[A-Za-z]{2,}$`
	reg := regexp.MustCompile(pattern)
	return reg.MatchString(email)
}

// ValidatePhoneByIsoCode 根据国家 ISO2 国家码验证全球手机号
func ValidatePhoneByIsoCode(phone, iosCode string) bool {
	pattern := isoCodeRegexp[iosCode]
	if pattern == "" {
		pattern = `^(\+?(\d{0,4})?\-?)?\d{7,11}$` // 其他国家，+国家码-7~11位长度的数字 就行
	}
	reg := regexp.MustCompile(pattern)
	return reg.MatchString(phone)
}

// ValidatePhoneByPhoneCode 根据国家手机区号验证全球手机号
func ValidatePhoneByPhoneCode(phone, phoneCode string) bool {
	pattern := phoneCodeRegexp[phoneCode]
	if pattern == "" {
		pattern = `^(\+?(\d{0,4})?\-?)?\d{7,11}$` // 其他国家，+国家码-7~11位长度的数字 就行
	}
	reg := regexp.MustCompile(pattern)
	return reg.MatchString(phone)
}
